<template>
  <ContentWrap>
    <Search :schema="searchSchema" @reset="setSearchParams" @search="setSearchParams" />
    <Table
      :columns="tableColumns"
      default-expand-all
      node-key="id"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total
      }"
      @register="tableRegister"
    />
  </ContentWrap>

  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <Write v-if="dialogVisible" ref="writeRef" :current-row="currentRow" />
    <template #footer>
      <BaseButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</BaseButton>
    </template>
  </Dialog>
</template>
<script setup lang="tsx">
import { reactive, ref, unref } from 'vue'
import { ElTag } from 'element-plus'
import { orderGoodsApi as requestApi } from '@/api/api'
import { useTable } from '@/hooks/web/useTable'
import { useI18n } from '@/hooks/web/useI18n'
import { Table, TableColumn } from '@/components/Table'
import { Search } from '@/components/Search'
import { FormSchema } from '@/components/Form'
import { ContentWrap } from '@/components/ContentWrap'
import Write from './components/Write.vue'
import { Dialog } from '@/components/Dialog'
import { BaseButton } from '@/components/Button'
requestApi.save({
  member_id: 1,
  goods: [
    {
      id: 6,
      count: 100
    },
    {
      id: 7,
      count: 90
    }
  ]
})
const { t } = useI18n()
const delId = ref('')
const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async (params: any) => {
    const res = await requestApi.page({
      ...params,
      ...searchParams.value
    })
    console.log(res)
    return {
      list: (res.data.rows || []).map((el) => {
        return {
          ...el
        }
      }),
      total: res.data.count
    }
  },
  fetchDelApi: async () => {
    console.log(delId.value)
    const res = await requestApi.del(unref(delId))
    return !!res
  }
})

const { dataList, loading, total } = tableState
const { getList, delList } = tableMethods

const tableColumns = reactive<TableColumn[]>([
  {
    field: 'index',
    label: t('active.index'),
    type: 'index'
  },
  {
    field: 'order_no',
    label: '订单号'
  },
  {
    field: 'status',
    label: t('banner.status'),
    slots: {
      default: (data: any) => {
        return (
          <>
            <ElTag type={data.row.status === 0 ? 'danger' : 'success'}>
              {data.row.status === 1 ? '已完成' : '进行中'}
            </ElTag>
          </>
        )
      }
    }
  },
  {
    field: 'create_time',
    label: t('active.createTime')
  },
  {
    field: 'action',
    label: t('userDemo.action'),
    width: 180,
    slots: {
      default: (data: any) => {
        const row = data.row
        return (
          <>
            <BaseButton type="primary" onClick={() => detailAction(row)}>
              {t('exampleDemo.detail')}
            </BaseButton>
            <BaseButton type="danger" onClick={() => delData(row)}>
              {t('exampleDemo.del')}
            </BaseButton>
          </>
        )
      }
    }
  }
])

const searchSchema = reactive<FormSchema[]>([
  {
    field: 'order_no',
    label: '订单编号',
    component: 'Input'
  }
])

const searchParams = ref({})
const setSearchParams = (data: any) => {
  searchParams.value = data
  getList()
}

const dialogVisible = ref(false)
const dialogTitle = ref('')

const currentRow = ref()

const writeRef = ref<ComponentRef<typeof Write>>()

const saveLoading = ref(false)

const detailAction = (row: any) => {
  dialogTitle.value = t('exampleDemo.detail')
  currentRow.value = row
  dialogVisible.value = true
}

const delData = async (row: any | null) => {
  console.log(row)
  delId.value = row.id
  console.log(delId.value)
  await delList()
}
</script>
